********************************************************************************
*
*		             Two tiers of admission, Table 4
*
********************************************************************************

clear 
use "${data}cleaned_data_y3.dta"
do "${blocks}construct_new_variables"

	egen ns_mean_perc_y3 = mean(Percentage_y3), by(Course_NatSci_split)
	egen ns_sd_perc_y3   = sd(Percentage_y3), by(Course_NatSci_split)
	gen ns_std_perc_y3   = (Percentage_y3 - ns_mean_perc_y3) / ns_sd_perc_y3

tempname statistics
tempfile stats
postfile `statistics' ///
    str100 statistic count pool y1pdiff y3pdiff using "`stats'", replace

	su WinterPool
	local count=r(N)
	local pool = r(mean)*100
	ttest  ns_std_perc_y1, by(WinterPool)
	local y1pdiff=r(mu_1)-r(mu_2)

	ttest  ns_std_perc_y3, by(WinterPool)
	local y3pdiff=r(mu_1)-r(mu_2)
	
post `statistics' ("All subjects") (`count') (`pool') (`y1pdiff') (`y3pdiff')

	ci mean WinterPool 
	local pool = r(se)*100
	local count = .
	
	ttest  ns_std_perc_y1, by(WinterPool)
	local y1diff=r(se)

	ttest  ns_std_perc_y3, by(WinterPool)
	local y3diff=r(se)

	post `statistics' ("\quad SE All")  (`count') (`pool') (`y1diff') (`y3diff')

	***
	
	replace Course_NatSci_split_str="Biological Natural Sciences" if Course_NatSci_split_str=="Biological NatSci"
	replace Course_NatSci_split_str="Physical Natural Sciences" if Course_NatSci_split_str=="Physical NatSci"

	local course_EEMP `""Economics" "Engineering" "Mathematics" "Physical Natural Sciences""'	
	local course_nEEMP `" "Law"  "Medicine" "Biological Natural Sciences" "'

	su WinterPool if EEMP==1
	
	local pool = r(mean)*100
	local count=r(N)
	ttest  ns_std_perc_y1 if EEMP==1, by(WinterPool)
	local y1pdiff=r(mu_1)-r(mu_2)

	ttest  ns_std_perc_y3 if EEMP==1, by(WinterPool)
	local y3pdiff=r(mu_1)-r(mu_2)

	post `statistics' ("")  (.) (.) (.) (.)
	
	post `statistics' ("MI subjects")  (`count') (`pool') (`y1pdiff') (`y3pdiff')
		
	ci mean WinterPool if EEMP==1
	local pool = r(se)*100
	local count = .
	
	ttest  ns_std_perc_y1 if EEMP==1, by(WinterPool)
	local y1diff=r(se)

	ttest  ns_std_perc_y3 if EEMP==1, by(WinterPool)
	local y3diff=r(se)

	post `statistics' ("\quad SE MI")  (`count') (`pool') (`y1diff') (`y3diff')
	
	su WinterPool if EEMP==0
	local pool = r(mean)*100
	local count=r(N)

	ttest  ns_std_perc_y1 if EEMP==0, by(WinterPool)
	local y1pdiff=r(mu_1)-r(mu_2)

	ttest  ns_std_perc_y3 if EEMP==0, by(WinterPool)
	local y3pdiff=r(mu_1)-r(mu_2)
	
	post `statistics' ("")  (.) (.) (.) (.)

	post `statistics' ("Non-MI subjects") (`count')   (`pool') (`y1pdiff') (`y3pdiff')
	
	ci mean WinterPool if EEMP==0
	local pool = r(se)*100
	local count = .
	
	ttest  ns_std_perc_y1 if EEMP==0, by(WinterPool)
	local y1diff=r(se)

	ttest  ns_std_perc_y3 if EEMP==0, by(WinterPool)
	local y3diff=r(se)

	post `statistics' ("\quad SE non-MI")  (`count') (`pool') (`y1diff') (`y3diff')

postclose `statistics'	
use `stats', clear
format pool* %9.1fc
format *diff %9.2fc
list


listtab * using "${tables}descriptives_pool.tex", ///
    rstyle(tabular) replace ///
    head("\begin{tabular}{@{\extracolsep{2pt}} l c c c c D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3} D{.}{.}{-3}}" ///
    "\toprule" ///
	    " & \multicolumn{1}{c}{\multirow{3}{*}{N}} & \multicolumn{1}{c}{\multirow{3}{*}{Taken from the pool (\%)}} & \multicolumn{2}{c}{Exam score differences} \\" ///
    " & & & \multicolumn{2}{c}{Direct - Pool} \\" ///
	"\cmidrule(lr){4-5}" ///
	"& & & \multicolumn{1}{c}{Year 1} & \multicolumn{1}{c}{Year 3} \\" ///
	"\midrule" "& (1) & (2) & (3) & (4) \\ \midrule") ///
    foot("\bottomrule" "\end{tabular}")
